<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form class="antd-modal-form" v-bind="formItemLayout" ref="formRef">
          <a-row>
						<a-col :span="12">
							<a-form-item label="采样点" v-bind="validateInfos.samplingPoint">
								<a-input v-model:value="formData.samplingPoint" placeholder="请输入采样点"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="分析项目" v-bind="validateInfos.analysisItem">
								<a-input v-model:value="formData.analysisItem" placeholder="请输入分析项目" disabled  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="分析结果" v-bind="validateInfos.analysisResult">
								<a-input v-model:value="formData.analysisResult" placeholder="请输入分析结果"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="单位" v-bind="validateInfos.unit">
								<a-input v-model:value="formData.unit" placeholder="请输入单位" disabled  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="采样时间" v-bind="validateInfos.samplingTime">
								<a-date-picker placeholder="请选择采样时间"  v-model:value="formData.samplingTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="阶段" v-bind="validateInfos.phase">
								<j-dict-select-tag type='radio' v-model:value="formData.phase" dictCode="jobPhase" placeholder="请选择阶段"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="12">
							<a-form-item label="登记人" v-bind="validateInfos.registrant">
						<j-select-user v-model:value="formData.registrant"  allow-clear />
							</a-form-item>
						</a-col>
            <a-col :span="12">
            	<a-form-item label="分析结果图片" v-bind="validateInfos.img">
            		<j-image-upload :fileMax="0" v-model:value="formData.img" ></j-image-upload>
            	</a-form-item>
            </a-col>
            <a-divider>电子签名</a-divider>
						<a-col :span="12">
							<a-form-item label="分析人" v-bind="validateInfos.analysisMan">
								<j-image-upload :fileMax="0" v-model:value="formData.analysisMan" disabled></j-image-upload>
                <SignaturePad width="1000" height="200" @ok="successUploadSign"/>
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, onMounted, inject, defineProps, unref } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectUser from '/@/components/Form/src/jeecg/components/JSelectUser.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { tbJobTicketGasAnalysisSaveOrUpdate } from '../TbJobTicket.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  import SignaturePad from '/@/components/chart/SignaturePad.vue';

  //接收主表id
  const mainId = inject('mainId');
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
        samplingPoint: '',   
        analysisItem: '',   
        analysisResult: '',   
        unit: '',   
        samplingTime: '',   
        phase: '',   
        registrant: '',   
        analysisMan: '',   
        img: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });
  const props = defineProps({
    disabled: { type: Boolean, default: false },
  });
  const formItemLayout = {
    labelCol: { xs: { span: 24 }, sm: { span: 5 } },
    wrapperCol: { xs: { span: 24 }, sm: { span: 16 } },
  };
  
  /**
   * 新增
   */
  function add() {
    edit({});
  }
  
  function successUploadSign(signImgUrl){
    //将组件回传的签名上传地址赋值给签批人字段，并做数据入库操作
    formData.analysisMan = signImgUrl;
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData,tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
   
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    if (unref(mainId)) {
      model['jobTicketId'] = unref(mainId);
    }
    await tbJobTicketGasAnalysisSaveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
